<?php
declare (strict_types = 1);

namespace app\api\validate;

use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
//        'email' => ['require','regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/'],
        'username' => ['require','length'=>'6,20'],
        'password' => ['require','length'=>'6,20','regex' => '/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+$/'],
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
//        'email.require' => '邮箱不能为空',
//        'email.regex' => '邮箱格式不符合',
//        'email.unique' => '邮箱已被注册',
        'username.require' => '用户名不能为空',
        'username.unique' => '用户名已被注册',
        'password.require' => '密码不能为空',
        'password.length' => '密码大于6个字符',
        'password.regex' => '密码必须是字母和数字的组合',
    ];
    /**
     *
     * @return User
     */
    public function sceneRegister()
    {
        return $this->only(['username','password'])
            ->append('username','unique:api_user,username');
    }
    protected $scene = [
        'login'  =>  ['username','password'],
    ];
}
